其他
ggplot2高度自定义绘制条形图
欢迎关注R语言数据分析指南
❝本节来介绍如何使用「ggplot2」在条形图上自定义添加数据点,下面通过一个小栗子来进行展示,基于此图可以衍化出更多实用的图表,后续在慢慢介绍
❞
安装R包并加载
package.list=c("tidyverse","ggtext")
for (package in package.list) {
if (!require(package,character.only=T, quietly=T)) {
install.packages(package)
library(package, character.only=T)
}
}
自定义颜色
red <- "#EE3224"
blue <- "#007DB2"
加载数据进行清洗
df <- readr::read_csv('transit_cost.txt') %>%
filter(country %in% c("US", "CA")) %>%
mutate(country = case_when(country=="US" ~ "U.S.",
country=="CA" ~ "Canada"),
cost_km_millions = as.numeric(cost_km_millions)) %>%
filter(!is.na(cost_km_millions), cost_km_millions > 0,
!is.na(stations), stations > 0,end_year <= 2021) %>%
select("country", "city", "line", "length", "stations",
"cost_km_millions", "end_year") %>%
arrange(desc(cost_km_millions)) %>%
mutate(name = paste0("**", line,"** | ", city),
name = fct_reorder(name, cost_km_millions))
构建需要添加点&线的数据集
df_stations <- df %>%
mutate(n_stations = stations) %>%
uncount(stations) %>%
group_by(name) %>%
mutate(row_num = row_number()) %>%
ungroup() %>%
mutate(
length_frac = length/(2*(n_stations + 1)),
station_pos = length_frac*2*row_num)
df_lines <- df %>%
mutate(zero = 0, start=0.5, end=length) %>%
select(name, country, start, end,zero, cost_km_millions) %>%
pivot_longer(cols=c(start, end, zero), names_to="v")
数据可视化
ggplot() + geom_line(
data = df_lines %>% filter(v %in% c("zero", "start")),
aes(x=value, y=name, group=name,
size=cost_km_millions, color=country)) +
geom_line(data = df_lines%>% filter(v %in% c("start", "end")),
aes(x=value, y=name, group=name, size=cost_km_millions, color=country),
lineend="round") +
geom_point(data = df_stations,aes(x=station_pos,y=name,
size=cost_km_millions, color=country),
fill="white",shape=22) +
scale_color_manual(values=c("U.S." = blue, "Canada" = red)) +
theme_classic() +
labs(X=NULL,y=NULL)+
theme(
legend.position = "none",
axis.text.y = ggtext::element_markdown(family=font,size=10, color="grey30"),
axis.text.x = element_text(family=font,size=10, color="grey30"),
axis.title = ggtext::element_markdown(family=font, color="grey30"),
panel.grid.major.x =element_blank(),
panel.grid.major.y = element_blank(),
plot.margin = margin(0,20,0,0))+
scale_x_continuous(expand=expansion(0))
❝好了今天的介绍到此结束,转发此文到朋友圈或者对此文打赏任意金额即可获取数据及代码,感谢各位的支持
❞
欢迎大家扫描下方二位码加入「QQ交流群」,与全国各地上千位小伙伴交流
「关注下方公众号下回更新不迷路」添加作者微信请备注单位+方向+姓名
2022-01-26
2022-01-25
2022-01-24
2022-01-18
2022-01-17
2022-01-14